/**
 * 扑克牌
 * label 名称，value 值，suit 花色 52张+大小王共 54 张
 * @access private
 * @var array
 */
function poker()
{
	this._cards = Array();
	var _cards_label = Array('2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A');
	var _i = 0;
	for (var _key in _cards_label) {// 黑桃
		this._cards[_i] = { 'label':_cards_label[_key] , 'value':Number(_key)+2 ,'suit':'SPADE' }
		_i++;
	}
	for (var _key in _cards_label) {// 红心
		this._cards[_i] = { 'label':_cards_label[_key] , 'value':Number(_key)+2 ,'suit':'HEART' }
		_i++;
	}
	for (var _key in _cards_label) {// 梅花
		this._cards[_i] = { 'label':_cards_label[_key] , 'value':Number(_key)+2 ,'suit':'CLUB' }
		_i++;
	}
	for (var _key in _cards_label) {// 方块
		this._cards[_i] = { 'label':_cards_label[_key] , 'value':Number(_key)+2 ,'suit':'DIAMOND' }
		_i++;
	}
	this._cards[_i]  = { 'label':'BLACK_JOKER', 'value':0, 'suit':'NULL' };
	_i++;
	this._cards[_i]  = { 'label':'RED_JOKER', 'value':0, 'suit':'NULL' };
}

/**
* 获取扑克牌
* {@source }  
* @access public
* @return array array
*/
poker.prototype.get = function()
{
	return this._cards.concat();
}

/**
* 移除指定的扑克牌
* {@source }  
* @access public
* @param array r_remove_cards 要移除的扑克牌，可以移除多张
* @return empty empty
*/	
poker.prototype.remove = function(r_remove_cards) 
{
	var _new_cards = Array();
	var _remove_card;	
	for (var _key in this._cards)
	{
		_val = this._cards[_key];
		for (var _r_key in r_remove_cards) {
			_r_val = r_remove_cards[_r_key];
			if ((_r_val['label'] == 'BLACK_JOKER' || _r_val['label'] == 'RED_JOKER') && (_val['value'] == _r_val['value']) ) {
				this._cards.splice(_key, 1);
			}else if ((_val['value'] == _r_val['value']) &&  (_val['suit'] == _r_val['suit']) ) {
				this._cards.splice(_key, 1);
				//r_remove_cards.splice(_r_key,1);
			}
		}
	}

}

/**
* 洗牌
* <b>作用：</b>
* 把扑克牌随机排序
* {@source }  
* @access public
* @return empty empty
*/
poker.prototype.shuffle = function() 
{
	var _index_array = Array();
	var _endIndex = this._cards.length;	
	for (var _i = 0; _i <= _endIndex - 1; _i++) {
		var _cards_long = this._cards.length;	
		var index = Math.random() * _cards_long;
		index = Math.floor(index);
		var _element = this._cards[index];
		this._cards.splice(index, 1);
		_index_array[_i] = _element;	
	}
	this._cards = _index_array;
}

/**
* 设定扑克牌
* {@source }  
* @access public
* @param array r_cards 要设定的扑克牌
* @return empty empty
*/
poker.prototype.set = function(r_cards)
{
	this._cards = r_cards;
	return;
}

/**
* 发牌
* <b>作用：</b>
* 1、从扑克牌中最后面的位置取出指定数量的牌并作为结果返回；
* 2、已发的扑克牌从源牌中移除；
* {@source }  
* @access public
* @param integer r_amount 发牌的数量，默认 1 张
* @return array | booleam 成功，返回发牌数据结果，失败返回 false
*/
poker.prototype.deal = function(r_amount)
{
	if (r_amount == null) return;
	var _cards_long = this._cards.length;
	if ( _cards_long < r_amount)
	{
		return false;
	}
	var _cards = [];
	var _index = _cards_long - r_amount;
	_cards = this._cards.slice(_index); //从_index的位置到末尾截取数组元素
	this._cards.splice(_index); //删除源牌中相应的元素
	_cards.reverse();  //将数组倒序
	return _cards;
}

/**
* 排序
* <b>作用：</b>
* 对扑克牌进行排序
* {@source }  
* @access public
* @param array r_cards 要排序的扑克牌
* @param string r_mode 要排序的方式，默认 ASC 从小到大升序，DESC 为从大到小降序
* @return array array 排序后的扑克牌
*/
poker.prototype.sort = function(r_cards, r_mode)
{
	if (!r_cards) return;
	var _cards_sort = Array();
	if('DESC' == r_mode){ 
		r_cards.sort(_descOrder);
	}else {
		r_cards.sort(_ascOrder);
	}		
	return r_cards;		
}
/**
* 数组排序方法sort调用的排序函数，从小到大排序
* 当返回值为1时，参数a对应元素排到参数b对应元素后
* 当返回值为-1时，参数b对应元素排到参数a对应元素前
* 当返回值为0时，参数a与参数b位置保持不变	 
* @param   r_a_element 
* @param   r_b_element
* @return  int int
*/
function _ascOrder(r_a_element, r_b_element)
{
	if (r_a_element['value'] < r_b_element['value']) {
		return -1;
	}else if(r_a_element['value'] > r_b_element['value']){
		return 1;
	}
}	
/**
* 数组排序方法sort调用的排序函数，从大到小排序
* 当返回值为1时，参数a对应元素排到参数b对应元素后
* 当返回值为-1时，参数b对应元素排到参数a对应元素前
* 当返回值为0时，参数a与参数b位置保持不变	 
* @param   r_a_element 
* @param   r_b_element
* @return  int int
*/
function _descOrder(r_a_element, r_b_element)
{
	if (r_a_element['value'] <= r_b_element['value']) {
		return 1;
	}else if(r_a_element['value'] > r_b_element['value']){
		return -1;
	}
}
	
   
   
